
#pragma once

#include "Utils.h"

class ATL_NO_VTABLE CEventSink :
	public CComObjectRootEx<CComSingleThreadModel>,
	public INetworkListManagerEvents,
	public INetworkEvents, 
	public INetworkConnectionEvents
{
private:
	DWORD                     m_dwCookie;
	CComPtr<IConnectionPoint> m_pConnectionPoint;
	GUID                      m_riid;
	HANDLE                    m_hThread;
	DWORD                     m_dwThreadId;

	HRESULT ListenForEvents();
	static DWORD WINAPI StartListeningForEventsThread(LPVOID pArg);

public:
	HWND m_hWndNotify;
	static HRESULT StartListeningForEvents(REFIID riid, CEventSink** ppEventResponse, HWND hWndNotify=NULL);
	HRESULT StopListeningForEvents();

protected:
	CEventSink()
	{
		m_hWndNotify = NULL;
		m_dwCookie = 0;
		m_riid = GUID_NULL;
		m_hThread = NULL;
		m_dwThreadId = 0;
	}

	DECLARE_NO_REGISTRY()

	BEGIN_COM_MAP(CEventSink)
		COM_INTERFACE_ENTRY(INetworkListManagerEvents)
		COM_INTERFACE_ENTRY(INetworkEvents)
		COM_INTERFACE_ENTRY(INetworkConnectionEvents)
	END_COM_MAP()

	DECLARE_PROTECT_FINAL_CONSTRUCT()

	HRESULT FinalConstruct()
	{
		return S_OK;
	}

	void FinalRelease()
	{
	}

	// INetworkListManagerEvents
	STDMETHOD(ConnectivityChanged)(NLM_CONNECTIVITY NewConnectivity);

	// INetworkEvents
	STDMETHOD(NetworkAdded)(GUID networkId);
	STDMETHOD(NetworkDeleted)(GUID networkId);
	STDMETHOD(NetworkConnectivityChanged)(GUID networkId,  NLM_CONNECTIVITY newConnectivity);
	STDMETHOD(NetworkPropertyChanged)(GUID networkId, NLM_NETWORK_PROPERTY_CHANGE flags);

	// INetworkConnectionEvents
	STDMETHOD(NetworkConnectionConnectivityChanged)(GUID networkId,  NLM_CONNECTIVITY newConnectivity);
	STDMETHOD(NetworkConnectionPropertyChanged)(GUID networkId, NLM_CONNECTION_PROPERTY_CHANGE flags);
};
